package com.restaurant.android.service;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.restaurant.domain.Employeecredential;
import com.restaurant.serviceimpl.EmployeecredentialServiceImpl;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * Servlet implementation class LoginVerifierServlet
 */
@WebServlet("/LoginVerifierServlet")
public class LoginVerifierServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginVerifierServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
	
	private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		XStream objXStream = new XStream(new DomDriver());
		objXStream.alias("loginData", Employeecredential.class);
		Employeecredential employeecredential = (Employeecredential)objXStream.fromXML(request.getParameter("loginData"));
		
		ArrayList<Employeecredential> listEmployeecredentials = new ArrayList<Employeecredential>();
		listEmployeecredentials = (ArrayList<Employeecredential>) new EmployeecredentialServiceImpl().loadByHql("from Employeecredential where pin = " + employeecredential.getPin());
		
		objXStream.alias("isExists", Boolean.class);
		
		if(listEmployeecredentials.size() == 1){
			response.getWriter().print(objXStream.toXML(new Boolean(true)));
		}else{
			response.getWriter().print(objXStream.toXML(new Boolean(false)));
		}
		
	}

}
